package com.managerProvider.service.impl;

import com.managerProvider.mapper.PowerMapper;
import com.managerProvider.mapper.PrMapper;
import com.managerProvider.service.PowerService;
import entity.*;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 权限服务impl
 * The type Power service.
 *
 * @author tapoliuliyue
 * @date 2022/04/23
 */
@Service
public class PowerServiceImpl implements PowerService {
    @Autowired
    private PowerMapper powerMapper;
    @Autowired
    private PrMapper prMapper;
    private Logger logger = Logger.getLogger(this.getClass());

    @Override
    public ResponseMessage selectAllPower() {
        ResponseMessage msg = new ResponseMessage();
        try {
            List<Power> powers = powerMapper.selectList(null);
            if (powers == null || powers.size() == 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("select power result is empty");
                logger.debug("PowerServiceImpl selectAllPower result:" + powers);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setFlag(true);
            msg.setData(powers);
            msg.setMessage("ok");
            logger.debug("PowerServiceImpl selectAllPower result:" + powers);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("select power result is error");
            logger.error("PowerServiceImpl selectAllPower result:" + e);
            return msg;
        }
    }

    @Override
    public ResponseMessage selectBySelf() {
        ResponseMessage msg = new ResponseMessage();
        try {
            List<Power> powers = powerMapper.selectBySelf();
            if (powers == null || powers.size() == 0) {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                msg.setMessage("select power result is empty");
                logger.debug("PowerServiceImpl selectBySelf result:" + powers);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setFlag(true);
            msg.setData(powers);
            msg.setMessage("ok");
            logger.debug("PowerServiceImpl selectBySelf result:" + powers);
            return msg;
        } catch (Exception e) {
            e.printStackTrace();
            msg.setCode(StatusCode.ERROR);
            msg.setFlag(false);
            msg.setMessage("select power result is error");
            logger.error("PowerServiceImpl selectBySelf result:" + e);
            return msg;
        }
    }

    @Override
    public ResponseMessage updatePr(Role role) {
        ResponseMessage msg = new ResponseMessage();
        //获取roleId
        String id = role.getId();
        //获取前端传来的权限id集合
        List<String> forwordList = role.getForwordList();
        if (forwordList != null && forwordList.size() > 0) {
            //获取所有权限集合
            List<Power> powerList = powerMapper.selectList(null);
            List<String> updateList = new ArrayList<>();
            //将所有权限集合和前端传来的权限集合进行循环判断，将前端集合数据和所有权限集合数据相同及权限集合的类型为子权限的挑出来
            for (int i = 0; i < forwordList.size(); i++) {
                for (int i1 = 0; i1 < powerList.size(); i1++) {
                    if (forwordList.get(i).equals(powerList.get(i1).getId()) && powerList.get(i1).getPowerType().equals("2")) {
                        updateList.add(powerList.get(i1).getId());
                    }
                }
            }
            if (updateList.size() > 0 && updateList != null) {
                prMapper.deletePr(id);
                for (int i = 0; i < updateList.size(); i++) {
                    Pr pr = new Pr();
                    pr.setRoleId(id);
                    pr.setPowerId(updateList.get(i));
                    prMapper.insert(pr);
                }
            } else {
                msg.setCode(StatusCode.ERROR);
                msg.setFlag(false);
                return msg;
            }
            msg.setCode(StatusCode.OK);
            msg.setFlag(true);
            return msg;
        }
        msg.setCode(StatusCode.ERROR);
        msg.setFlag(false);
        return msg;
    }
}
